#include "map.h"
#include "linkedlist.h"
#include "../logic/ghost.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

// 地图初始数据
static char map_data[21][20] = {
    "####################",
    "#....#.....#.....#.#",
    "#....#.....#.....#.#",
    "#..................#",
    "#..##..####..##...#",
    "#....#.....#.....#.#",
    "#....#.....#.....#.#",
    "######.#.##.#.######",
    "#    #.#.##.#.#    #",
    "######.#.##.#.######",
    "#.................#",
    "######.#.##.#.######",
    "#    #.#.##.#.#    #",
    "######.#.##.#.######",
    "#....#.....#.....#.#",
    "#....#.....#.....#.#",
    "#..##..####..##...#",
    "#..................#",
    "#....#.....#.....#.#",
    "#....#.....#.....#.#",
    "####################"
};

void map_init(Map* map) {
    // 初始化地图数据
    int i, j;  // 声明在外部
    for (i = 0; i < MAP_ROWS; i++) {
        for (j = 0; j < MAP_COLS; j++) {
            map->grid[i][j] = map_data[i][j];
        }
    }
    // 初始化豆子链表
    dotlist_init(&map->dotHead);
    map->dotCount = 0;
    // 构建豆子链表
    for (i = 0; i < MAP_ROWS; i++) {
        for (j = 0; j < MAP_COLS; j++) {
            if (map->grid[i][j] == DOT) {
                dotlist_insert(&map->dotHead, i, j);
                map->dotCount++;
            }
        }
    }
}

void map_draw(Map* map) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    int ghost_color_idx = 0;
    const WORD ghost_colors[] = {
        FOREGROUND_RED | FOREGROUND_INTENSITY,                    // 红色
        FOREGROUND_BLUE | FOREGROUND_INTENSITY,                   // 蓝色
        FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, // 黄色
        FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY // 白色
    };

    for (int i = 0; i < MAP_ROWS; i++) {
        for (int j = 0; j < MAP_COLS; j++) {
            char c = map->grid[i][j];
            if (c == WALL) {
                // 判断四周，决定用哪种墙体字符
                int up = (i > 0 && map->grid[i-1][j] == WALL);
                int down = (i < MAP_ROWS-1 && map->grid[i+1][j] == WALL);
                int left = (j > 0 && map->grid[i][j-1] == WALL);
                int right = (j < MAP_COLS-1 && map->grid[i][j+1] == WALL);
                
                SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                if (up || down) printf("|");
                else if (left || right) printf("-");
                else printf("+");
                
            } else if (c == DOT) {
                SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                printf(".");
            } else if (c == PACMAN) {
                SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                printf("C");
            } else if (c == GHOST) {
                SetConsoleTextAttribute(hConsole, ghost_colors[ghost_color_idx]);
                printf("G");
                ghost_color_idx = (ghost_color_idx + 1) % 4;
            } else {
                printf(" ");
            }
        }
        ghost_color_idx = 0;
        printf("\n");
    }
    // 恢复默认颜色
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

int map_eat_dot(Map* map, int row, int col) {
    if (map->grid[row][col] != DOT) {
        return 0;
    }
    // 从链表中移除豆子
    int removed = dotlist_remove(&map->dotHead, row, col);
    if (removed) {
        map->dotCount--;
        map->grid[row][col] = EMPTY;
        return 1;
    }
    return 0;
}

void map_free_dots(Map* map) {
    dotlist_free(&map->dotHead);
    map->dotCount = 0;
}

int map_is_valid_position(Map* map, int row, int col) {
    if (row < 0 || row >= MAP_ROWS || col < 0 || col >= MAP_COLS) {
        return 0;
    }
    return map->grid[row][col] != WALL;
} 